package tjs.study.notes.eureka.clienta.order;

import com.codingapi.txlcn.tc.config.EnableDistributedTransaction;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableEurekaClient
@SpringBootApplication
@Configuration
@EnableDistributedTransaction(enableTxc = false)
@EnableTransactionManagement
public class OrderApplication {
    public static String INSTANCE_ID="";
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(OrderApplication.class, args);
        INSTANCE_ID = applicationContext.getEnvironment().getProperty("eureka.instance.instance-id");
    }


    @Bean("jdbcTemplate")
    public JdbcTemplate initJdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(this.initBasicDataSource());
        return jdbcTemplate;
    }
    @Bean
    public DataSourceTransactionManager initTransactionManager() {
        org.springframework.jdbc.datasource.DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(this.initBasicDataSource());
        return transactionManager;
    }

    @Bean("dataSource")
    public BasicDataSource initBasicDataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setUrl("jdbc:mysql://localhost:3306/tjs_mydemo");
        basicDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("qx123456");
        return basicDataSource;
    }

}
